package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ScoreRankListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ScoreRank;
import com.isunland.managesystem.entity.ScoreRankOriginal;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/17 16:26
 */
public class ScoreRankListFragment extends BaseListFragment {
  private static final String EXTRA_TYPE =
      ScoreRankListActivity.class.getSimpleName() + "EXTRA_TYPE";
  public static final String MONTH = "month";
  public static final String YEAR = "year";
  private String type;
  private ArrayList<ScoreRank> mList;
  private int mPage;
  private ScoreRankListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    type = getArguments().getString(EXTRA_TYPE);
    mList = new ArrayList<>();
    mPage = 1;
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_SCORE_RANK_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    //当月排名
    if (MONTH.equalsIgnoreCase(type)) {
      params.put("type", "month");
    }
    //年度排名
    if (YEAR.equalsIgnoreCase(type)) {
      params.put("type", "year");
    }
    params.put("page", mPage + "");
    params.put("pageSize", "20");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    ScoreRankOriginal original = new Gson().fromJson(json, ScoreRankOriginal.class);
    ArrayList<ScoreRank> rows = original.getRows();

    if (1 == original.getResult() && original.getRows() != null) {
      if (rows != null && rows.size() == 0) {
        ToastUtil.show(R.string.that_is_all);
      }
      //获取列表成功
      if (mPage == 1) {
        mList.clear();
      }
      mList.addAll(rows);
      if (mAdapter == null) {
        mAdapter = new ScoreRankListAdapter(getActivity(), mList, type);
        setListAdapter(mAdapter);
        return;
      }
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {

    if (PullToRefreshBase.Mode.PULL_FROM_START == refreshView.getCurrentMode()) {
      mPage = 1;
    }
    if (PullToRefreshBase.Mode.PULL_FROM_END == refreshView.getCurrentMode()) {
      mPage++;
    }
    volleyPost();
  }

  public static ScoreRankListFragment newInstance(String type) {
    Bundle args = new Bundle();
    args.putString(EXTRA_TYPE, type);
    ScoreRankListFragment fragment = new ScoreRankListFragment();
    fragment.setArguments(args);
    fragment.setArguments(args);
    return fragment;
  }
}
